<html>
<head>
<script type="text/javascript">
	function Insert() {
		document.getElementById("classid").required = true;
		document.getElementById("action").value = "insert";
	}
	function Delete(row) {
		document.getElementById("action").value = "delete";
		document.getElementById("row").value = row;

	}
</script>
</head>
<body>
	<%@ page language="java" import="java.sql.*"%>
	<%
		try {
			Class.forName("org.postgresql.Driver");
			Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/cse132b","cse132blogin", "1234567890");
			String action = request.getParameter("action");
			// Check if an insertion is requested
			if (action != null && action.equals("insert")) {
				PreparedStatement pstmt;
				pstmt = conn.prepareStatement("INSERT INTO sections (class_id) VALUES (?)");
				pstmt.setInt(1, Integer.parseInt(request.getParameter("classid")));
				int rowCount = pstmt.executeUpdate();
				out.println("Successfully made new section.<br/>");
			} else if (action != null && action.equals("delete")) {
				PreparedStatement pstmt;
				pstmt = conn.prepareStatement("DELETE FROM sections WHERE section_id = ?");
				pstmt.setInt(1, Integer.parseInt(request.getParameter("row")));
				pstmt.executeUpdate();
			}

			// Create the statement
			PreparedStatement sections;
			sections = conn.prepareStatement("SELECT * FROM sections WHERE class_id = ?");
			sections.setInt(1, Integer.parseInt(request.getParameter("classid")));

			// Use the created statement to SELECT
			// the student attributes FROM the Student table.
			ResultSet rsSections = sections.executeQuery();
			%>
	<table border="1">
		<tr>
			<td valign="top" width="40%">
				<%-- -------- Include menu HTML code -------- --%> <jsp:include
					page="/menu.html" />
			</td>
			<td>	
	<table border="0">
		<tr>
			<td valign="top">Sections Entry Page</td>
		</tr>
		<tr>
			<form action="sections.jsp" method="post">
			<input type="hidden" id="action" name="action"/>
			<input type="hidden" id="row" name="row"/>
			<input type="hidden" id="classid" name="classid" value="<%=request.getParameter("classid") %>"/>
			<table border="1">
				<tr>
					<th>Section Id</td>
				</tr>
				<% while (rsSections.next())  { %>
				<tr>
					<td><%=rsSections.getInt("section_id") %></td>
					<td><a href="meetings.jsp?sectionid=<%=rsSections.getInt("section_id") %>">Edit Meetings</a></td>
					<td><input type="submit" onclick="Delete(<%=rsSections.getInt("section_id") %>)" value="Delete"/></td>
				</tr>
				<% } %>
				<tr>
					<td></td>
					<td><input type="submit" onclick="Insert()" value="Add New"/></td>
				</tr>
			</table>
		</tr>
	</table>
	</form>
	<%
		} catch (SQLException ex) {
			out.print("SQLException: " + ex.getMessage() + "<br/>");
			out.print("SQLState: " + ex.getSQLState() + "<br/>");
			out.print("VendorError: " + ex.getErrorCode() + "<br/>");
		}
	%>
</td>
</tr>
</table>
</body>
</html>